package com.ftg.learn.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * @author 布衣
 */
@RestControllerAdvice
@Slf4j
public class GlobalAdvice {

    /**
     * sql异常处理
     * @param sqlException  异常处理
     * @return R<String>
     */
    @ExceptionHandler(SQLException.class)
    public R<String> handleSqlException(SQLException sqlException) {
        log.error("sql语句异常: " + sqlException.getMessage());
        return R.build(5100, "sql语句异常: " + sqlException.getMessage(), null);
    }

    /**
     * 运行时异常处理
     * @param runtimeException  异常处理
     * @return R<String>
     */
    @ExceptionHandler(RuntimeException.class)
    public R<String> handleRuntimeException(RuntimeException runtimeException) {
        runtimeException.printStackTrace();;
        log.error("运行时异常: " + runtimeException.getMessage());
        return R.build(5300, "运行时异常: " + runtimeException.getMessage(), null);
    }
}
